//
//  LZMyPersonInfoTableView.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/10/28.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyPersonInfoTableView.h"
#import "LZMyPersonInfoCommonTableViewCell.h"

#import "Masonry.h"
#import "LZUserModel.h"
#import "UIImageView+WebCache.h"
#import "GVUserDefaults+LZProperties.h"

@interface LZMyPersonInfoTableView() <UITableViewDelegate, UITableViewDataSource>

@end

@implementation LZMyPersonInfoTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self setup];
    }
    return self;
}

#pragma
#pragma mark private method

- (void)setup {
    self.backgroundColor = UIColorFromRGB(0xf9f7f7,1);
    self.separatorColor = [UIColor clearColor];
    self.dataSource = self;
    self.delegate = self;
}

#pragma
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 3;
    }
    else {
        if ([GVUserDefaults standardUserDefaults].isSetPassword) {
            return 3;
        } else {
            return 1;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LZMyPersonInfoCommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    if (!cell) {
        cell = [[LZMyPersonInfoCommonTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                        reuseIdentifier:@"CellIdentifier"
                                                                andType:LZ_MY_INFO_CELL_TYPE_HEADIMAGE];
    }
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        cell.type = LZ_MY_INFO_CELL_TYPE_HEADIMAGE;
        cell.subTitleLabel.text = LZLocalizedString(@"personal_set", nil);
        cell.headImageView.layer.masksToBounds = YES;
        cell.headImageView.layer.cornerRadius = 65/2.0;
        [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", APIBaseImageUrl, [LZUserModel Instance].merchantInfo.sellerPhoto]]
                              placeholderImage:[UIImage imageNamed:@"lz_userinfo_headerimage"]];
    }
    else {
        cell.type = LZ_MY_INFO_CELL_TYPE_TITLE;
        
        if (indexPath.section == 0 && indexPath.row == 1) {
            cell.titleLabel.text = self.dataArrays[0];
            cell.subTitleLabel.text = LZLocalizedString(@"complete", nil);
        }
        else if (indexPath.section == 0 && indexPath.row == 2) {
            cell.titleLabel.text = self.dataArrays[1];
            cell.subTitleLabel.text = LZLocalizedString(@"edit_min", nil);
        }
        else if(indexPath.section == 1 && indexPath.row == 0){
            cell.titleLabel.text = self.dataArrays[indexPath.row+2];
            if ([GVUserDefaults standardUserDefaults].isSetPassword) {
                cell.subTitleLabel.text = LZLocalizedString(@"change_setting", nil);
            }else{
                cell.subTitleLabel.text = LZLocalizedString(@"personal_set", nil);
            }
            
        }else if (indexPath.section == 1 && indexPath.row == 1){
            cell.titleLabel.text = self.dataArrays[indexPath.row+2];
            if ([GVUserDefaults standardUserDefaults].cellphone.length > 0) {
//                cell.subTitleLabel.text = LZLocalizedString(@"change_setting", nil);
                cell.subTitleLabel.text = [GVUserDefaults standardUserDefaults].cellphone;
            }else{
                cell.subTitleLabel.text = LZLocalizedString(@"personal_set", nil);
            }
        
        }else if (indexPath.section == 1 && indexPath.row == 2){
            cell.titleLabel.text = self.dataArrays[indexPath.row+2];
            if ([GVUserDefaults standardUserDefaults].email.length > 0) {
//                cell.subTitleLabel.text = LZLocalizedString(@"change_setting", nil);
                cell.subTitleLabel.text = [GVUserDefaults standardUserDefaults].email;
            }else{
                cell.subTitleLabel.text = LZLocalizedString(@"personal_set", nil);
            }
        
        }
    }
    
    return cell;
}

#pragma
#pragma mark UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    if (section == 0) {
        UIView * v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        v.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
        return v;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section == 0) {
        return 10.0f;
    }
    
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 85.0f;
        }
        else
            return 50.0f;
    }
    else
        return 50.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(_didSelectRowBlock) {
        _didSelectRowBlock(indexPath);
    }
}

@end
